import torch
import torchaudio
from transformers import AutoProcessor, SeamlessM4TModel

# 使用 CPU 或 CUDA
device = "cuda" if torch.cuda.is_available() else "cpu"

# 加载 processor 和 model（注意 use_fast=False）
processor = AutoProcessor.from_pretrained("facebook/seamless-m4t-v2-large", use_fast=False)
model = SeamlessM4TModel.from_pretrained("facebook/seamless-m4t-v2-large").to(device)

# 加载音频（注意音频格式）
waveform, sample_rate = torchaudio.load("test_audio.wav")
if sample_rate != 16000:
    waveform = torchaudio.functional.resample(waveform, sample_rate, 16000)

# 做推理
inputs = processor(audios=waveform, src_lang="eng", return_tensors="pt").to(device)
with torch.no_grad():
    output = model.generate(**inputs, task="asr")

# 解码结果
text = processor.batch_decode(output, skip_special_tokens=True)[0]
print("识别文本：", text)
